package com.atao.baokede.service.serviceImpl;


import cn.hutool.core.bean.BeanUtil;
import com.atao.baokede.mapper.ChargeOrderMapper;
import com.atao.baokede.mapper.UserMapper;
import com.atao.baokede.pojo.ChargeOrder;
import com.atao.baokede.pojo.vo.ChargeOrderVo;
import com.atao.baokede.pojo.vo.ChargeOrderVo2;
import com.atao.baokede.service.ChargeOrderService;
import com.atao.baokede.utils.OrderIdUtil;
import com.atao.baokede.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.time.LocalDateTime.now;

@Service
public class ChargeOrderServiceImpl implements ChargeOrderService {
    @Autowired
    private ChargeOrderMapper chargeOrderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ChargeOrderVo add(BigDecimal  amount) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Long userId=((Number) map.get("id")).longValue();
        ChargeOrder chargeOrder = new ChargeOrder();
        chargeOrder.setUserId(userId);
        chargeOrder.setOrderNo(Long.valueOf(OrderIdUtil.generateOrderId()));
        chargeOrder.setStatus(0);
        chargeOrder.setRequestAmount(amount);
        chargeOrder.setCreatedTime(LocalDateTime.now());
        chargeOrderMapper.insert(chargeOrder);

        //对象属性拷贝,将chargeOrder中的属性值拷贝到chargeOrderVo中，用BeanUtils
        ChargeOrderVo chargeOrderVo = new ChargeOrderVo();
        BeanUtil.copyProperties(chargeOrder, chargeOrderVo);
        chargeOrderVo.setUsername(userMapper.findByUserId(userId).getUsername());
        return chargeOrderVo;
    }


    @Override
    public ChargeOrderVo modify(ChargeOrder chargeOrder) {
        //得到用户id
        Long userId = chargeOrder.getUserId();
        //修改订单,并得到修改后的结果
        chargeOrderMapper.updateById(chargeOrder);
        ChargeOrder c = chargeOrderMapper.selectByOrderNo(Long.valueOf(chargeOrder.getOrderNo()));
        //用BeanUtils属性拷贝成Vo
        ChargeOrderVo chargeOrderVo = new ChargeOrderVo();
        BeanUtils.copyProperties(c, chargeOrderVo);
        chargeOrderVo.setUsername(userMapper.findByUserId(userId).getUsername());
        userMapper.updateBalance(c.getRequestAmount(), userId);
        return chargeOrderVo;
    }

    @Override
    public ChargeOrder selectByOrderNo(Long orderNo) {
        return chargeOrderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<ChargeOrderVo2> findAll() {
        Map<String, Object> map= ThreadLocalUtil.get();
        Long userId=((Number)map.get("id")).longValue();
        List<ChargeOrder> chargeOrders = chargeOrderMapper.findAllByUserId(userId);
        return BeanUtil.copyToList(chargeOrders, ChargeOrderVo2.class);
    }

    @Override
    public void delete(Long orderNo) {
        chargeOrderMapper.deleteById(orderNo);
    }

    @Override
    public void updateStatus(Long orderNo, int statusRefunded) {
        chargeOrderMapper.updateStatus(orderNo, statusRefunded);
    }
}
